package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.AssessmentMapper;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.PsychologyMapper;
import com.itheima.mapper.RiskMapper;
import com.itheima.pojo.Member;
import com.itheima.service.PsychologyService;
import com.itheima.service.RiskService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RiskService.class)
@Transactional
public class RiskServiceImpl implements RiskService {
    @Autowired
    private RiskMapper riskMapper;
    @Autowired
    private PsychologyMapper psychologyMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private AssessmentMapper assessmentMapper;

    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        //mybatis分页助手
        PageHelper.startPage(currentPage,pageSize);
        //根据条件查询
        Page<Map> page = riskMapper.findPage(queryString);
        long total = page.getTotal();
        List<Map> result = page.getResult();
        for (Map map : result) {
            //处理时间
            Date DateTime = (Date) map.get("regTime");
            try {
                String regTime = DateUtils.parseDate2String(DateTime);
                map.put("regTime",regTime);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //定义报告状态
            String status = (String) map.get("status");
            if (status == null){
                map.put("status","未出报告");
            }else {
                map.put("status","风险报告已出");
            }
//            if (key.equals("status")){
//                    map.put("status","风险报告已出");
//                } else {
//                    map.put("status","未出报告");
//                }
//            }
        }
        return new PageResult(total,result);
    }

    //获取文件名
    public String findFileName(String fileNumber) {
        //根据用户档案号查询用户id
        //根据用户id查询文件名
        String fileName = riskMapper.findFileName(fileNumber);
        return fileName;
    }

    //获取编辑窗口回显
    public Map getMember(String fileNumber) {
        Map map = riskMapper.getMemberAndRisk(fileNumber);
        for (Object key : map.keySet()) {
            if (key.equals("regTime")) {
                Date DateTime = (Date) map.get(key);
                try {
                    String regTime = DateUtils.parseDate2String(DateTime);
                    map.put("regTime",regTime);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return map;
    }

//    编辑会员风险报告
    public void add(Map map) {
        riskMapper.add(map);
    }

    //删除信息
    public Map delete(String fileNumber) {
        //根据会员档案查询会员id
        Member member = memberMapper.getMember(fileNumber);
        Integer memberId = member.getId();
        //根据会员id删除order表
        memberMapper.deleteOrderByMemberId(memberId);
        //查询该会员的体检报告名称
        Map fileNameMap = memberMapper.findFileName(memberId);
        //根据会员id删除体检报告表
        memberMapper.deleteDoc(memberId);

        //删除会员的健康测评记录--体质
        assessmentMapper.deleteMember(memberId);
        //删除会员的健康测评记录--心理
        psychologyMapper.deleteMember(memberId);
        //删除会员的健康测评记录--风险
        riskMapper.deleteMember(memberId);

        //根据会员id删除会员
        memberMapper.deleteById(memberId);
        return fileNameMap;
    }

    //获取风险报告数据
    public Map<String, Object> getRisk(String fileNumber) {
        //根据会员档案查询数据
        Map<String, Object> risk = riskMapper.getRisk(fileNumber);
        Date dateTime = (Date) risk.get("rigTime");
        try {
            String regTime = DateUtils.parseDate2String(dateTime);
            risk.put("regTime",regTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return risk;
    }

}
